package com.yd.mvc.pc.OldsRetrieveController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.OldsRetrieveService.OldsRetrieveService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Old;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.OldParam;
import com.yd.domain.sys.DicEnum;
import com.yd.domain.sys.Logger;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author zy
 * @create 2018-03-08 10:23
 * @desc
 **/
@RestController
@RequestMapping(value="/api/oldsRetrieve")
public class OldsRetrieveController extends BaseController {
    @Autowired
    private OldsRetrieveService oldsRetrieveService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc: 录入旧物消息
     * @Date:  2018/3/8
     * @Param
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addDic(@RequestBody Old old,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.OLD_MANAGER);//验证权限
            userName=user.getUserName();
        }
        String suff="ID:"+old.getId()+";名称:"+old.getTitle()+";重量:"+old.getPerWeight();
        try {
            old.setAddUser(user.getUserId());//录入人
            oldsRetrieveService.addOld(old);
            loggerService.addLogger(new Logger("新增旧物记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增旧物记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:查询数据旧物回收列表
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/getList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getOldList(@RequestBody OldParam oldParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=oldsRetrieveService.getOldList(oldParam,pageIndex,pageSize);
        return pm;
    }

    /**
     * 查询旧物
     * @param id
     */
    @RequestMapping(value = "/getOldById/{id}", method = RequestMethod.GET)
    public Old getGoodsById(@PathVariable("id")String id)  {
        Old old=oldsRetrieveService.getOldById(id);
        return old;
    }
    /**
     * @Author: zy
     * @Desc: 数据修改
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateOld(@RequestBody Old old,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+old.getId()+";名称:"+old.getTitle()+";重量:"+old.getPerWeight()+";商品类型:"+old.getType();
        try {
        oldsRetrieveService.updateOld(old);
        loggerService.addLogger(new Logger("修改旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
    } catch (Exception e) {
        loggerService.addLogger(new Logger("修改旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 删除数据
     * @Date:  2018/3/8
     * @Param
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteOld(@PathVariable("id") String id,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+id;
        try {
        oldsRetrieveService.deleteOld(id);
        loggerService.addLogger(new Logger("删除旧物记录",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
        loggerService.addLogger(new Logger("删除旧物记录",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 修改旧物状态:审核 ,并且设置价格等
     * 审核人、审核时间
     * @Date:  2018/3/8
     * @Param
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public void updateStatus(@RequestBody Old o,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+o.getId()+";名称:"+o.getTitle()+";重量:"+o.getPerWeight()+";审核状态:"+o.getStatus()+";价格:"+o.getPrice();
        try {
            o.setCheckUser(user);//审核人
            o.setCheckTime(new Date());
            oldsRetrieveService.updateStaus(o);
            loggerService.addLogger(new Logger("审核旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("审核旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:交易状态、交易时间、最终价格
     * @Date:  2018/3/8
     * @Param
    */
    @RequestMapping(value = "/updateDealStatus", method = RequestMethod.POST)
    public void updateDealStatus(@RequestBody Old o,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+o.getId()+";名称:"+o.getTitle()+";重量:"+o.getPerWeight()+";交易状态:"+o.getDealStatus()+";价格:"+o.getPrice()+";交易时间:"+o.getDealTime();
        try {
           // o.setDealTime(new Date());
            oldsRetrieveService.updateDealStaus(o);
            loggerService.addLogger(new Logger("旧物交易记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
        loggerService.addLogger(new Logger("旧物交易记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
         e.printStackTrace();
        }
    }

    /**
     * 审核旧物
     * @param oldId
     * @param checkStatus
     */
    @RequestMapping(value = "/checkStatus/{oldId}/{checkStatus}", method = RequestMethod.GET)
    public void checkStatus(@PathVariable("oldId")String oldId,@PathVariable("checkStatus")String checkStatus)  {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        oldsRetrieveService.updateOldkStatus(oldId,user.getUserId(),new Date(),checkStatus);
    }

    /**
     * 旧物估价
     * @param o
     */
    @RequestMapping(value = "/assessOld", method = RequestMethod.POST)
    public void assessOld(@RequestBody Old o)  {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        oldsRetrieveService.assessOld(o);
    }
    /**
     * 审核旧物
     * @param oldId
     * @param dealStatus
     */
    @RequestMapping(value = "/updateDealStatus/{oldId}/{dealStatus}", method = RequestMethod.GET)
    public void updateDealStatus(@PathVariable("oldId")String oldId,@PathVariable("dealStatus")String dealStatus)  {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        oldsRetrieveService.updateDealStatus(oldId,new Date(),dealStatus);
    }

    /**
     * 删除图片
     * @param id
     */
    @RequestMapping(value = "/updateIconUrl/{id}", method = RequestMethod.GET)
    public void updateIconUrl(@PathVariable("id")String id)  {
        oldsRetrieveService.updateIconUrl(id);
    }
}
